//
//  ViewController.m
//  15-HomeWork
//
//  Created by dllo on 16/8/3.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "CustomCollectionViewCell.h"
#import "CustomCollectionViewLayout.h"
#import <AVFoundation/AVFoundation.h>
#define kCollectionViewCellReuse @"reuse"

@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,CustomCollectionViewLayoutDelegate>
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)NSMutableArray *titleArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self handData];
    [self creacteCollectionView];
}
- (void)handData {
    _titleArray = [NSMutableArray arrayWithObjects:@"头条",@"精选", @"娱乐", @"体育", @"网易号",@"本地", @"视频", @"财经", @"科技", @"汽车" ,@"时尚", @"图片", @"直播", @"热点", @"跟帖", @"房产", @"轻松一刻", @"段子", @"军事", @"历史", nil];
}

- (void)creacteCollectionView {
//     UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//    CGFloat width = (CGRectGetWidth(self.view.frame) - 15) / 2;
//    
//    layout.itemSize = CGSizeMake(width, 120);
//    layout.minimumLineSpacing = 5;
//    layout.minimumInteritemSpacing = 5;
//    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    CustomCollectionViewLayout *layout = [[CustomCollectionViewLayout alloc] init];
    layout.backGroundColor = [UIColor redColor];
    layout.delegate = self;
    layout.cellHeight = 80;
    _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    
    [_collectionView registerNib:[UINib nibWithNibName:@"CustomCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kCollectionViewCellReuse];
}


#pragma mark - CustomCollectionViewLayoutDelegate
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout height:(CGFloat)height widthForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *string = _titleArray[indexPath.row];
    CGRect currentRect = [string boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
    currentRect.size.width = ceil(currentRect.size.width);
    
    return currentRect.size.width;
}


#pragma mark - CollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _titleArray.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
   CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCellReuse forIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
    cell.titleLabel.text = _titleArray[indexPath.row];
//    cell.titleLabel.font = [UIFont systemFontOfSize:13];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
